<script setup>
import { ElMessage } from 'element-plus';
import Layer from "@/components/layer/index.vue";
import { defineProps, ref, defineEmits, onMounted } from 'vue'
import { addAPI, updateAPI } from '@/api/wedding/noticeInfo'
import { userAll } from '@/api/system/user';
// 使用defineEmits来接收来自父组件的方法
const emit = defineEmits(['getTableData'])
// 声明props
const props = defineProps({
  layer: {
    type: Object,
    default: () => {
      return {
        show: false,
        title: '',
        showButton: true
      }
    }
  }
});

/************************************定义变量************************************************/
const ruleForm = ref(null)
const layerDom = ref(null)
const rules = {
  userName: [{ required: true, message: '请选择接收人', trigger: 'blur' }],
  notice: [{ required: true, message: '请输入消息内容', trigger: 'blur' }],
  type: [{ required: true, message: '请输入类型：', trigger: 'blur' }],
}
let form = ref({
  id: null,
  userId: null,
  userName: null,
  notice: null,
  type: null,
  creator: null,
  createTime: null,
  updateTime: null,
  delFlag: null,
})

/************************************数据回显************************************************/
if (props.layer.row) {
  if (props.layer.row) {
    let source = props.layer.row
    Object.keys(form.value).forEach(k => k in source && (form.value[k] = source[k]));
  }
}

/************************************定义事件************************************************/
//提交按钮事件
let submit = () => {
  if (ruleForm) {
    ruleForm.value.validate((valid) => {
      if (valid) {
        let params = form.value
        if (props.layer.row) {
          updateForm(params)
        } else {
          addForm(params)
        }
      } else {
        return false;
      }
    });
  }
}

let updateForm = (params) => {
  updateAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '编辑成功'
      })
      emit('getTableData', false)
      layerDom && layerDom.value.close()
    })
}

let addForm = (params) => {
  addAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '新增成功'
      })
      emit('getTableData', true)
      layerDom && layerDom.value.close()
    })
}
/************************************获取用户列表************************************************/
const userList = ref([]);
onMounted(() => {
  // 获取用户列表
  userAll().then(res => {
    console.log('[ res ] >', res)
    userList.value = res.data;
    console.log('[ userList ] >', userList)
  });
});
</script>

<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="接收人：" prop="userName">
        <el-select v-model="form.userId" placeholder="请选择接收人">
          <el-option v-for="item in userList" :key="item.id" :label="item.userName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型：" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型">
          <el-option label="平台消息" value="0"></el-option>
          <el-option label="用户协议" value="1"></el-option>
          <el-option label="隐私政策" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消息内容：" prop="notice">
        <el-input type="textarea" v-model="form.notice" placeholder="请输入消息内容"></el-input>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<style scoped lang="scss">
.el-input-number--large {
  line-height: 38px;
  width: 230px;
}
</style>